﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System;

namespace Hont.HyperWigglePackage.Editor
{
    public class MotionBuilder_Move : BaseWigglePlugins
    {
        PRPair[] mCacheData;
        float mXValue;
        float mYValue;
        float mZValue;
        AnimationCurve mCurve;
        Camera mCamera;

        Vector3 mCameraCachePosition;
        Quaternion mCameraCacheRotation;

        bool mIsLocked;


        public override void Execute(HyperWiggleEditorCore core)
        {
            base.Execute(core);

            mCamera = mCore.View.PreviewRenderUtility.camera;

            mCameraCachePosition = mCamera.transform.position;
            mCameraCacheRotation = mCamera.transform.rotation;

            mCore.IsStopCameraControl = true;

            mCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(0.5f, 1), new Keyframe(1, 0));
        }

        protected override void OnExit()
        {
            mCore.IsStopCameraControl = false;
        }

        protected override void OnGUI()
        {
            mCurve = EditorGUILayout.CurveField("Curve: ", mCurve);
            mXValue = EditorGUILayout.Slider("X Value: ", mXValue, -10f, 10f);
            mYValue = EditorGUILayout.Slider("Y Value: ", mYValue, -10f, 10f);
            mZValue = EditorGUILayout.Slider("Z Value: ", mZValue, -10f, 10f);

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Cancel"))
            {
                mXValue = 0;
                mYValue = 0;
                mZValue = 0;
                mCamera.transform.position = mCameraCachePosition;
                mCamera.transform.rotation = mCameraCacheRotation;
                mCacheData.CopyTo(mCore.FrameData, 0);

                mCore.UpdatePreview();
                mCore.UpdateTimeline();
                mIsLocked = false;
                mCore.IsStopCameraControl = true;
            }

            if (GUILayout.Button("Apply"))
            {
                mCacheData = new PRPair[mCore.FrameData.Length];
                mCore.FrameData.CopyTo(mCacheData, 0);

                var lastPosition = mCameraCachePosition;

                for (int i = 0; i < mCore.FrameData.Length; i++)
                {
                    var current = i / (float)mCore.EndFrame;
                    var lerp = mCurve.Evaluate(current);
                    var currentX = mXValue * lerp;
                    var currentY = mYValue * lerp;
                    var currentZ = mZValue * lerp;
                    var currentPosition = mCameraCachePosition + new Vector3(currentX, currentY, currentZ);
                    var positionOffset = currentPosition - lastPosition;

                    mCore.FrameData[i].RelativePosition += positionOffset;

                    lastPosition = currentPosition;
                }

                HyperWiggleUtil.StableOffset(mCore.FrameData);

                mCore.UpdatePreview();
                mCore.UpdateTimeline();
                mIsLocked = true;
                mCore.IsStopCameraControl = false;
            }

            GUILayout.EndHorizontal();

            if (!mIsLocked)
            {
                mCamera.transform.position = mCameraCachePosition + new Vector3(mXValue, mYValue, mZValue);
            }
        }
    }
}